import crypto from "crypto";

export function generateRandomString(min:number, max:number): string {
    const length = Math.round(Math.random() * (max - min)) + min;
    const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
    let result = '';
    for (let i = 0; i < length; i++) {
        result += chars.charAt(Math.floor(Math.random() * chars.length));
    }
    return result;
}

function generateAccessKeyId(): string {
    // Generate a random string as the AccessKey ID
    return generateRandomString(20,24)
}

function generateSecretKey(): string {
    // Generate a random 密钥
    return generateRandomString(12,15);
}

function generateAccessKeySecret(accessKeyId: string, secretKey: string): string {
    // Generate a signature for the AccessKey ID using the secret key
    const hmac = crypto.createHmac("sha256", secretKey);
    hmac.update(accessKeyId);
    // 将 Access Key ID 和签名结合起来形成 Access Key Secret
    return hmac.digest("base64")
}

// Generate the AccessKey ID and AccessKey Secret
const accessKeyId = generateAccessKeyId();
const secretKey = generateSecretKey();
const signature = generateAccessKeySecret(secretKey, accessKeyId);

// Print the AccessKey ID and AccessKey Secret
console.log(`AccessKey ID: ${accessKeyId}`);
console.log(`secretKey ID: ${secretKey}`);
console.log(`req.headers.authorization: ${accessKeyId}:${signature}`);

